package test.my.com.myapplicationtest.fliker_line_progress;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import test.my.com.myapplicationtest.R;

/**
 * Author : Cooper
 * Time : 2017/5/7  15:54
 * Description : 直线闪烁滑块view
 */

public class FlikerLine extends View {

    private int mWidth = 100,mHeight = 100;
    private int mLineColor; // 线的颜色
    private int mFlikerSlipDuration; // 滑块滑动所需时间
    private int mFlikerImage; // 滑块图片资源
    private int mLineRadius; // 线的圆角度数

    private Paint mLinePaint;
    private RectF mLineRectF;

    /**
     * 左右来回移动的滑块
     */
    private Bitmap flikerBitmap;

    /**
     * 滑块移动最左边位置，作用是控制移动
     */
    private float flickerLeft;

    private Paint flikerPaint;

    /**
     * 进度条 bitmap ，包含滑块
     */
    private Bitmap pgBitmap;

    private Canvas pgCanvas;
    private ValueAnimator mValueAnimator;

    private BitmapShader bitmapShader;

    public FlikerLine(Context context) {
        super(context);
    }

    public FlikerLine(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlikerLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FlikerLine,defStyleAttr,0);
        mLineColor = typedArray.getColor(R.styleable.FlikerLine_lineColor, Color.BLUE);
        mFlikerSlipDuration = typedArray.getInteger(R.styleable.FlikerLine_flikerSlipDuration, 1000);
        mFlikerImage = typedArray.getResourceId(R.styleable.FlikerLine_flikerImage, R.drawable.flicker);
        mLineRadius = (int) typedArray.getDimension(R.styleable.FlikerLine_LineRadius, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()));
        typedArray.recycle();

        init();
    }

    private void init() {
        mLinePaint = new Paint();
        mLinePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setStyle(Paint.Style.FILL);
        mLinePaint.setColor(mLineColor);

        flikerPaint = new Paint();
        flikerPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

        mLineRectF = new RectF();
    }

    private void initPgBimap() {
        flikerBitmap = BitmapFactory.decodeResource(getResources(), mFlikerImage);
        flickerLeft = 0;

        pgBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        pgCanvas = new Canvas(pgBitmap);
    }

    private void initAnimator() {
        mValueAnimator = ValueAnimator.ofFloat(-flikerBitmap.getWidth(),mWidth);
        mValueAnimator.setRepeatMode(ValueAnimator.RESTART);
        mValueAnimator.setDuration(mFlikerSlipDuration);
        mValueAnimator.setRepeatCount(Integer.MAX_VALUE);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                flickerLeft = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        mValueAnimator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        pgCanvas.drawRect(mLineRectF,mLinePaint);
        pgCanvas.drawBitmap(flikerBitmap,flickerLeft,0,mLinePaint);

        // 画圆角
        bitmapShader = new BitmapShader(pgBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        flikerPaint.setShader(bitmapShader);
        canvas.drawRoundRect(mLineRectF, mLineRadius, mLineRadius, flikerPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = measureWidth(widthMeasureSpec);
        mHeight = measureHeight(heightMeasureSpec);
        setMeasuredDimension(mWidth,mHeight);
        mLineRectF.set(0,0,mWidth,mHeight);
        initPgBimap();
        initAnimator();
    }

    private int measureWidth(int widthMeasureSpec) {
        int result = 0;
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            result = widthSize;
        } else {
            result = mWidth;
            if (widthMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, widthSize);
            }
        }
        return result;
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            result = heightSize;
        } else {
            result = mWidth;
            if (heightMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, heightSize);
            }
        }
        return result;
    }

}
